package team.se.tms.controller.stage;

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.controller.BaseController;
import team.se.tms.dto.Page;
import team.se.tms.dto.StageChain;
import team.se.tms.exception.DeleteException;
import team.se.tms.exception.InsertException;
import team.se.tms.service.StageInfoService;
import team.se.tms.util.ResultUtil;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("stage")
public class StageInfoController extends BaseController {

    @Autowired
    StageInfoService stageInfoService;

    /**
     * 去查询阶段链的界面
     * @return String
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @RequestMapping("list")
    public String tosearchStageLine() throws Exception{
        return "stage/stage_line_list";
    }
    /**
     * 去添加阶段链的界面
     * @return ModelAndView
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
   @RequestMapping("toAdd")
   public ModelAndView toAddStageChain(){
       ModelAndView mv = this.getModelAndView();
       mv.addObject("dotype","add");
       mv.setViewName("stage/modify_stage");
       return mv;
   }
    /**
     * 去编辑阶段链的界面
     * @param firstStageId 阶段链第一个id
     * @return ModelAndView
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @RequestMapping("toEdit")
    public ModelAndView toEditStageChain(String firstStageId,HttpSession session){
        ModelAndView mv = this.getModelAndView();
        StageChain stageChain =null;
        List<StageChain> stageChainList =(List<StageChain>)session.getAttribute("stageChainList");
        for(StageChain item:stageChainList){
            if(firstStageId.equals(item.getFirstStageId())){
                stageChain = item;
                break;
            }
        }
        Gson gson = new Gson();
        mv.addObject("stageChain",gson.toJson(stageChain));
        mv.addObject("dotype","edit");
        mv.setViewName("stage/modify_stage");
        return mv;
    }
    /**
     * 添加阶段链
     *
     * @param stageChain 阶段链信息
     * @return Object 添加结果
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
   @RequestMapping("add")
   @ResponseBody
   public Object addStageChain(@RequestBody StageChain stageChain) throws Exception{

       boolean result  = stageInfoService.saveStageChain(stageChain.getStageInfoList());
       if(!result){
           throw new InsertException("添加阶段链失败");
       }
       return ResultUtil.success("添加阶段链成功");
   }
    /**
     * 查询所有的阶段链
     *
     * @param page
     * @return String
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
   @RequestMapping("getList")
   @ResponseBody
   public String searchAllStageChains(Page page, HttpSession session) throws Exception{
       List<StageChain> stageChainList = stageInfoService.listStageChains(page);
       session.setAttribute("stageChainList",stageChainList);
       return page.getDataTabelJson(stageChainList);
   }

    /**
     * 修改阶段链
     *
     * @param stageChain 阶段链信息
     * @return Object 修改结果
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @RequestMapping("edit")
    @ResponseBody
    public Object editStageChain(@RequestBody StageChain stageChain) throws Exception{

        boolean result  = stageInfoService.modifyStageChain(stageChain.getFirstStageId(),stageChain.getStageInfoList());
        if(!result){
            throw new InsertException("修改阶段链失败");
        }
        return ResultUtil.success("修改阶段链成功");
    }


    @RequestMapping("drop")
    @ResponseBody
    public Object dropStageChain(String stageId) throws Exception{
        Boolean success = stageInfoService.removeStageInfos(stageId);
        if (!success){
            throw new DeleteException();
        }
        return ResultUtil.success("删除成功");
    }
}
